public class 顺时针打印矩阵 {
    public int[] clockwisePrint(int[][] mat, int n, int m) {
        int[] ret = new int[n * m];
        int startX = 0;
        int endX = n - 1;
        int startY = 0;
        int endY = m - 1;
        int index = 0;
        while (startX <= endX && startY <= endY) {
            // 打印行
            for (int i = startY; i <= endY; i++) {
                ret[index++] = mat[startX][i];
            }
            // 打印列
            for (int i = startX + 1; i <= endX; i++) {
                ret[index++] = mat[i][endY];
            }

            if (startX < endX) {
                for (int i = endY - 1; i >= startY; i--) {
                    ret[index++] = mat[endX][i];
                }
            }
            if (startY < endY) {
                for (int i = endX - 1; i > startX; i--) {
                    ret[index++] = mat[i][startY];
                }
            }
            startX++;
            startY++;
            endY--;
            endX--;
        }
        return ret;
    }
}